package StudyCode.summary.myfile;

import java.io.*;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CopiesSourceFileToTargetFolder {

	public static void main(String[] args) throws IOException {
		copyFile("E:\\迅雷下载\\test1.txt", "E:\\Copy");
	}

	private static void copyFile(String sourceFile, String targetFolder) throws FileNotFoundException, IOException {
		assert (new File(sourceFile).isFile());
		assert (new File(targetFolder).isDirectory());

		File source = new File(sourceFile);
		copyFile(source.getParent(), targetFolder, source.getName());
	}

	private static void copyFile(String sourceFolder, String targetFolder, String name) throws FileNotFoundException, IOException {
		copyFile(sourceFolder, targetFolder, name, name);
	}

	private static void copyFile(String sourceFolder, String targetFolder, String sourceName, String targetName) throws FileNotFoundException, IOException {
		InputStream is = new FileInputStream(sourceFolder + File.separator
				+ sourceName);
		OutputStream os = new FileOutputStream(targetFolder
				+ File.separator + targetName);
		byte[] buffer = new byte[102400];
		while (true) {
			int len = is.read(buffer);
			if (len < 0)
				break;
			os.write(buffer, 0, len);
		}
		System.out.println("复制成功。");
		is.close();
		os.close();
	}

}
